class stu:
	def __init__(self):
		import pymysql
		self.db=pymysql.connect(host="localhost",user="root",password="w6398725",db="mydemo",charset="utf8")
		self.cursor=self.db.cursor()
	def findall(self):
		self.sql="select * from stu"
		try:
			self.cursor.execute(self.sql)
			while True:
				self.data=self.cursor.fetchone()
				if self.data==None:
					break
				print(self.data)	
			print("成功操作行数：",self.cursor.rowcount)
		except Exception as err:
			print("sql语言报错，错误为：",err)

	def deletedata(self,dataname):
		self.dataname=dataname
		self.sql1="select * from stu where name = '%s'"%(self.dataname)
		self.sql2="delete from stu where name = '%s'"%(self.dataname)
		try:
			self.cursor.execute(self.sql1)
			if self.cursor.fetchone == None:
				print("数据库中不存在该数据")
			else:
				self.cursor.execute(self.sql2)
				self.db.commit()
				print("成功删除行数：",self.cursor.rowcount)
		except Exception as err:
			self.db.rollback()
			print("sql语言报错，错误为：",err)
	def insertdata(self,name,age,sex,classid):
		#个人的stu数据库结构为姓名+年龄+性别+班级号
		self.name,self.age,self.sex,self.classid=name,age,sex,classid
		self.sql="insert into stu(name,age,sex,classid) values('%s','%d','%s','%s')"%(self.name,self.age,self.sex,self.classid)
		try:
			self.cursor.execute(self.sql)
			self.db.commit()
			print("成功添加行数：",self.cursor.rowcount)
		except Exception as err:
			self.db.rollback()
			print("sql语言报错，错误为：",err)
	def __del__(self):
		self.db.close()

studata=stu()
print("="*12,"学院管理系统","="*14)
print("{0:1}{1:13}{2:15}".format(" ","1.查看学员信息","2.添加学员信息"))
print("{0:1}{1:13}{2:15}".format(" ","3.删除学员信息","4.退出系统"))
print("="*40)
while True:
	key=input("请输入对应的选择:")
	if key == "1":
		print("="*12,"学员信息浏览","="*14)
		studata.findall()
	elif key == "2":
		print("="*12,"添加信息浏览","="*14)
		insdata=input("请输入添加账号信息,按：姓名，年龄，性别，班级 形式呈现：").split(",")
		studata.insertdata(insdata[0],int(insdata[1]),insdata[2],insdata[3])
	elif key == "3":
		print("="*12,"删除信息浏览","="*14)
		delename=input("请输入删除账号姓名:")
		studata.deletedata(delename)
	elif key == "4":
		print("="*14,"退出系统","="*14)
		del studata
		break
	else:
		print("输入有误！请重新输入")

